package com.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.mapper.AppMapper;
import com.mapper.AutoTradeMapper;
import com.mapper.GoodMapper;
import com.mapper.UsersMapper;
import com.mapper.WtdMapper;
import com.pojo.OUser;
import com.pojo.Statis;
import com.pojo.Wtd;
import com.pojo.trecord;

@Controller
@RequestMapping("/")
public class appcontroller {
	@Resource
	private UsersMapper usersMapper;
	@Resource
	private AutoTradeMapper autoTradeMapper;
	@Resource
	private WtdMapper wtdMapper;
	@Resource
	private AppMapper appMapper;
	@Resource
	private GoodMapper goodMapper;
	
	@RequestMapping("app")
	public String app(HttpServletRequest request, Model model) {
		return "jsp/app";
	}
	
	@RequestMapping("applogin")
	public String applogin(@Param("UserID") String UserID, @Param("Password") String Password,HttpServletRequest request, Model model) {
		HttpSession session = request.getSession();
		session.setAttribute("UserID", UserID);
		session.setAttribute("Password", Password);
		if (UserID == null || UserID == "") {
			return "jsp/app/applogin";
		} else if (Password == null || Password == "") {
			return "jsp/app/applogin";
		} 
		int ip=usersMapper.selectip(UserID);
		HashMap<String, Object> users = new HashMap<String, Object>();
		users.put("UserID", UserID.trim());
		users.put("Password", Password.trim());
		OUser user = usersMapper.getUserByUsernameAndPassword(users);
		session.setAttribute("user", user);
		if (user == null) {
			model.addAttribute("error", "用户名或密码错误！");
			return "jsp/app/applogin";
		} else if(ip==0) {
			model.addAttribute("error", "你的注册申请正在审核，请耐心等待");
			return "jsp/app/applogin";
		}else {
			// return "redirect:/main.html";
			return "redirect:/appmain.html?page=1&last=5";
		}
	}
	 
	@RequestMapping("appmain")
	public String appmain(HttpServletRequest request, Model model) {
		HttpSession session = request.getSession();
		OUser user = (OUser) session.getAttribute("user");
		String UserID = user.getUserID();
		//System.out.println("76"+UserID);
		//分页
		int page=Integer.parseInt((request.getParameter("page")));
		int last=Integer.parseInt((request.getParameter("last")));
		int begin=(page-1)*5;
		System.out.println("page="+page+"last="+last);
		
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("begin", begin);
		params.put("last", last);
		params.put("UserID", UserID);
		
		List list4= appMapper.searchques(UserID);
		//System.out.println(JSON.toJSONString(list4));
		trecord qb=(trecord)list4.get(0);
		request.setAttribute("countques", qb.getCountques());
		
		List<Map<String,String>> list = appMapper.selecttradeinfo(params);
		request.setAttribute("list", list);
		request.setAttribute("page", page);
		return "jsp/app/appindex";
		//return "redirect:/main.html";
	}
	
	@RequestMapping("appmember")
	public String appmember(HttpServletRequest request, Model model) {
		HttpSession session = request.getSession();
		OUser user = (OUser) session.getAttribute("user");
		String UserID = user.getUserID();
		OUser oo = usersMapper.selectmember(UserID);
		//System.out.println(oo.getRecast());
		request.setAttribute("list", oo);
		List list = new ArrayList();
		Map map = new HashMap();
		List li =wtdMapper.selectgoodstype();
		for(int i=0;i<li.size();i++) {
			Map mp = (Map)li.get(i);
			map.put("UserID", UserID);
			map.put("goods", mp.get("classname"));
			Wtd wtd=usersMapper.selectcgmx(map);
			if(wtd!=null) {
				list.add(wtd.getGoodnum()-wtd.getDjgoodnum());
			}
			else
			list.add(0);
		}
		request.setAttribute("list1", list);
		
		return "jsp/app/appmember";
		//return "redirect:/main.html";
	}
	
	@RequestMapping("reg")
	public String reg(HttpServletRequest request, Model model) {
		//System.out.println("success");
		return "jsp/app/reg";
		//return "redirect:/main.html";
	}
	
	@RequestMapping("appchongzhi_his")
	public String appchongzhi_his(HttpServletRequest request, Model model) {
		HttpSession session = request.getSession();
		OUser user = (OUser) session.getAttribute("user");
		String UserID = user.getUserID();
		//查询充值记录所有信息
		List<Map<String,String>> list =appMapper.selectczhis(UserID);
		request.setAttribute("list", list);
		
		return "jsp/app/appchongzhi_his";
		//return "redirect:/main.html";
	}
	
	@RequestMapping("apptixian_his")
	public String apptixian_his(HttpServletRequest request, Model model) {
		HttpSession session = request.getSession();
		OUser user = (OUser) session.getAttribute("user");
		String UserID = user.getUserID();
		//查询提现记录所有信息
		List<Map<String,String>> list =appMapper.selecttxhis(UserID);
		request.setAttribute("list", list);
		//System.out.println("success");
		return "jsp/app/apptixian_his";
		//return "redirect:/main.html";
	}
	
	@RequestMapping("appbuylist")
	public String appbuylist(HttpServletRequest request, Model model) {
		HttpSession session = request.getSession();
		OUser user = (OUser) session.getAttribute("user");
		String UserID = user.getUserID();
		//查询挂购买记录所有信息
		List<Map<String,String>> list =appMapper.selectblist(UserID);
		request.setAttribute("list", list);
		return "jsp/app/appbuylist";
		//return "redirect:/main.html";
	}
	
	@RequestMapping("appsellist")
	public String appsellist(HttpServletRequest request, Model model) {
		HttpSession session = request.getSession();
		OUser user = (OUser) session.getAttribute("user");
		String UserID = user.getUserID();
		//查询挂售出记录所有信息
		List<Map<String,String>> list =appMapper.selectslist(UserID);
		request.setAttribute("list", list);
		return "jsp/app/appsellist";
		//return "redirect:/main.html";
	}
	
	@RequestMapping("appgr_money")
	public String appgr_money(HttpServletRequest request, Model model) {
		HttpSession session = request.getSession();
		OUser user = (OUser) session.getAttribute("user");
		String UserID = user.getUserID();
		List<Map<String,String>> list=appMapper.selectgrmoney(UserID);
		request.setAttribute("list", list);
		return "jsp/app/appgr_money";
		//return "redirect:/main.html";
	}
	
	@RequestMapping("appchongzhi")
	public String appchongzhi(HttpServletRequest request, Model model) {
		HttpSession session = request.getSession();
		OUser user = (OUser) session.getAttribute("user");
		String UserID = user.getUserID();
		OUser oo = usersMapper.selectmember(UserID);
		//System.out.println(oo.getRecast());
		request.setAttribute("list", oo);
		return "jsp/app/appchongzhi";
		//return "redirect:/main.html";
	}
	
	@RequestMapping("apptixian")
	public String apptixian(HttpServletRequest request, Model model) {
		HttpSession session = request.getSession();
		OUser user = (OUser) session.getAttribute("user");
		String UserID = user.getUserID();
		OUser oo = usersMapper.selectmember(UserID);
		//System.out.println(oo.getEncash());
		request.setAttribute("list", oo);
		return "jsp/app/apptixian";
		//return "redirect:/main.html";
	}
	
	@RequestMapping("appguadan")
	public String appguadan(HttpServletRequest request, Model model) {
		String goods = request.getParameter("goods");
		HttpSession session = request.getSession();
		OUser user = (OUser) session.getAttribute("user");
		String UserID = user.getUserID();
		Map mp = new HashMap();
		mp.put("UserID", UserID);
		mp.put("goods", goods);
		//卖出
		List<Map<String,String>> list = appMapper.selectmaichu(mp);
		//买入
		List<Map<String,String>> list1 = appMapper.selectmairu(mp);
		request.setAttribute("list1", list1);
		request.setAttribute("list", list);
		
		OUser oo = usersMapper.selectmember(UserID);
		request.setAttribute("list3", oo);
		//查询多商品 下拉框
		List<Map> list2=wtdMapper.selectgoodstype();
		request.setAttribute("list2", list2);
		
		Statis price = goodMapper.selectopenprice(goods);//查询每日statis的开盘价
		request.setAttribute("price", price);
		request.setAttribute("goods", goods);
		return "jsp/app/appguadan";
	}
	@RequestMapping("appout")
	public String appout(HttpServletRequest request, Model model) {
		request.getSession().removeAttribute("user");
		request.getSession().invalidate();
		return "redirect:/applogin.html";
	}

	@RequestMapping("apprecord")
	public String apprecord(HttpServletRequest request, Model model) {
		HttpSession session = request.getSession();
		OUser user = (OUser) session.getAttribute("user");
		String UserID = user.getUserID();
		List<Map<String,String>> list=appMapper.selectrecord(UserID);
		request.setAttribute("list", list);
		return "jsp/app/apprecord";
		//return "redirect:/main.html";
	}
	//12 18 xml
	@RequestMapping("change")
	public String change(HttpServletRequest request, Model model) {
		HttpSession session = request.getSession();
		OUser Ouser = (OUser) session.getAttribute("user");
		OUser user = usersMapper.selectmember(Ouser.getUserID());
		request.setAttribute("user", user);
		//System.out.println("success");
		return "jsp/app/change";
		//return "redirect:/main.html";
	}
	// 修改密码
		@RequestMapping("xgmm")
		@ResponseBody
		public String xgmm(HttpServletRequest request, OUser m, Model model) {
			HttpSession session = request.getSession();
			OUser Ouser = (OUser) session.getAttribute("user");
			
			HashMap<String, Object> users = new HashMap<String, Object>();
			OUser user = usersMapper.selectmember(Ouser.getUserID());
			users.put("Password", m.getPassword1());
			users.put("UserID", user.getUserID());
			if (m.getPassword().equals(user.getPassword())) {
				usersMapper.updatepwd(users);
			} else {
				String s1 = "fpwd";
				return s1;
			}
			request.getSession().removeAttribute("user");
			request.getSession().invalidate();
			return "redirect:/applogin.html";
		}
		
/*		@RequestMapping("appmingxi")
		public String appmingxi(HttpServletRequest request, Model model) {
			HttpSession session = request.getSession();
			OUser user = (OUser) session.getAttribute("user");
			String UserID = user.getUserID();
			List<Map<String,String>> list=appMapper.selectgrmoney(UserID);
			request.setAttribute("list", list);
			return "jsp/app/appgr_money";
			//return "redirect:/main.html";
		}*/

}
